'use strict';
const dbHelper = require("db-helper")
const {
	wxPubConfig
} = require("config")
const WxPubApi = require("wx-public-api")
const wxPubApi = new WxPubApi()
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	let {
		queryStringParameters = {}
	} = event

	let city = event.city || queryStringParameters.city
	let sceneId = parseInt(event.sceneId) || parseInt(queryStringParameters.sceneId)
	let sceneStr = event.sceneStr || queryStringParameters.sceneStr
	let forever = event.forever || queryStringParameters.forever
	if (!city || (!sceneId && !sceneStr)) {
		return {
			code: 400,
			msg: "参数错误"
		}
	}
	try {
		let res = await wxPubApi.genQRCode(city, {
			sceneId,
			sceneStr,
			forever: forever == undefined ? false : !!forever
		})
		return {
			mpserverlessComposedResponse: true, // 使用阿里云返回集成响应是需要此字段为true
			isBase64Encoded: false,
			statusCode: 301,
			headers: {
				'location': res
			}
		}
	} catch (e) {
		console.error("获取公众号二维码遇到错误", e)
		return {
			code: 500,
			msg: e.message
		}
	}
}
